package com.tdam2012.grupo05;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class HistorialLlamadas extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_historial_llamadas);
        
        ListView lv = (ListView) findViewById(R.id.list);
        HistorialLlamadaAdapter adapter = new HistorialLlamadaAdapter(obtenerHistoriales());     
        lv.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_historial_llamadas, menu);
        return true;
    }

    private ArrayList<HistorialLLamada> obtenerHistoriales(){
        ArrayList<HistorialLLamada> Historiales = new ArrayList<HistorialLLamada>();
        
        Historiales.add(new HistorialLLamada("15/08/2012 09:50", "Entrante"));
        Historiales.add(new HistorialLLamada("10/09/2012 12:30", "Entrante"));
        Historiales.add(new HistorialLLamada("13/09/2012 13:55", "Saliente"));
        Historiales.add(new HistorialLLamada("17/09/2012 10:30", "Perdida"));
        Historiales.add(new HistorialLLamada("19/09/2012 16:30", "Entrante"));
        
        return Historiales;
    }
    
    class HistorialLLamada{
        String fechaHora;
        String descripcion;
        
        public HistorialLLamada (String fechaHora, String descripcion){
                this.fechaHora = fechaHora;
                this.descripcion = descripcion;
        }
        
        public String getFechaHora(){
                return this.fechaHora;
        }
        
        public String getDescripcion(){
                return this.descripcion;
        }
    }
    
    class Holder{
        TextView txtFechaHora, txtDescripcion; 
    }
    
    class HistorialLlamadaAdapter extends BaseAdapter{

        private ArrayList<HistorialLLamada> _Historiales;
        private LayoutInflater _inflater;
        
        public HistorialLlamadaAdapter(ArrayList<HistorialLLamada> Historiales){
                _Historiales = Historiales;
                _inflater = LayoutInflater.from(HistorialLlamadas.this);
        }
        
                public int getCount() {
                        // TODO Auto-generated method stub
                        return _Historiales.size();
                }

                public HistorialLLamada getItem(int position) {
                        // TODO Auto-generated method stub
                        return _Historiales.get(position);
                }

                public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return position;
                }

                public View getView(int position, View convertView, ViewGroup parent) {
                        // TODO Auto-generated method stub
                        Holder holder = null;
                        if (convertView == null) {
                                convertView = _inflater.inflate(R.layout.item_lista, null);
                                holder = new Holder();
                                
                                holder.txtFechaHora = (TextView) convertView
                                                .findViewById(R.id.textView1);
                                
                                holder.txtDescripcion = (TextView) convertView
                                                .findViewById(R.id.textView2);
                                                                
                                convertView.setTag(holder);
                        } else {
                                holder = (Holder) convertView.getTag();
                        }

                        HistorialLLamada Historial = _Historiales.get(position);

                        holder.txtFechaHora.setText(Historial.getFechaHora());
                        holder.txtDescripcion.setText(Historial.getDescripcion());

                        return convertView;
                }
        
    }
}
